<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>浅析commonjs中require的执行方式 | Jaho’s Blog</title>
    <meta name="generator" content="VuePress 1.8.0">
    <link rel="icon" href="/favicon.ico">
    <link rel="manifest" href="/manifest.json">
    <meta name="description" content="记录日常学习工作">
    
    <link rel="preload" href="/assets/css/0.styles.5b8fe0a4.css" as="style"><link rel="preload" href="/assets/js/app.6514719e.js" as="script"><link rel="preload" href="/assets/js/2.ea114ffb.js" as="script"><link rel="preload" href="/assets/js/27.204436be.js" as="script"><link rel="preload" href="/assets/js/4.e093c564.js" as="script"><link rel="prefetch" href="/assets/js/10.13203aa5.js"><link rel="prefetch" href="/assets/js/11.099868c4.js"><link rel="prefetch" href="/assets/js/12.c8365e17.js"><link rel="prefetch" href="/assets/js/13.89751e7c.js"><link rel="prefetch" href="/assets/js/14.31f563eb.js"><link rel="prefetch" href="/assets/js/15.08b381b6.js"><link rel="prefetch" href="/assets/js/16.1a3867de.js"><link rel="prefetch" href="/assets/js/17.7dd4e0c6.js"><link rel="prefetch" href="/assets/js/18.907c4597.js"><link rel="prefetch" href="/assets/js/19.fac8aa1a.js"><link rel="prefetch" href="/assets/js/20.73fe0e75.js"><link rel="prefetch" href="/assets/js/21.5c50aa1c.js"><link rel="prefetch" href="/assets/js/22.83d03663.js"><link rel="prefetch" href="/assets/js/23.061e7ac0.js"><link rel="prefetch" href="/assets/js/24.cc7e9011.js"><link rel="prefetch" href="/assets/js/25.fa6c7aef.js"><link rel="prefetch" href="/assets/js/26.39392f70.js"><link rel="prefetch" href="/assets/js/28.3f54c157.js"><link rel="prefetch" href="/assets/js/29.799d048c.js"><link rel="prefetch" href="/assets/js/3.6c5d6214.js"><link rel="prefetch" href="/assets/js/30.deed02f3.js"><link rel="prefetch" href="/assets/js/31.5916873b.js"><link rel="prefetch" href="/assets/js/32.b0634144.js"><link rel="prefetch" href="/assets/js/33.734675b8.js"><link rel="prefetch" href="/assets/js/34.20404dc4.js"><link rel="prefetch" href="/assets/js/35.d93b829f.js"><link rel="prefetch" href="/assets/js/36.b0692d8d.js"><link rel="prefetch" href="/assets/js/37.3dffa306.js"><link rel="prefetch" href="/assets/js/38.04649a97.js"><link rel="prefetch" href="/assets/js/39.7dfcdc32.js"><link rel="prefetch" href="/assets/js/40.04914511.js"><link rel="prefetch" href="/assets/js/41.ced85449.js"><link rel="prefetch" href="/assets/js/42.72739213.js"><link rel="prefetch" href="/assets/js/43.de1cff16.js"><link rel="prefetch" href="/assets/js/44.48e301ae.js"><link rel="prefetch" href="/assets/js/45.870a7650.js"><link rel="prefetch" href="/assets/js/46.657f15b8.js"><link rel="prefetch" href="/assets/js/47.3df1bfdb.js"><link rel="prefetch" href="/assets/js/48.f4e4e774.js"><link rel="prefetch" href="/assets/js/49.632bf611.js"><link rel="prefetch" href="/assets/js/5.6531e181.js"><link rel="prefetch" href="/assets/js/50.a8018493.js"><link rel="prefetch" href="/assets/js/51.cb424f59.js"><link rel="prefetch" href="/assets/js/52.a9f595dc.js"><link rel="prefetch" href="/assets/js/53.cf8a9ec0.js"><link rel="prefetch" href="/assets/js/54.46d774a3.js"><link rel="prefetch" href="/assets/js/55.3574f9b8.js"><link rel="prefetch" href="/assets/js/56.baf60ba5.js"><link rel="prefetch" href="/assets/js/57.af42b559.js"><link rel="prefetch" href="/assets/js/58.9300cb59.js"><link rel="prefetch" href="/assets/js/6.00f437ec.js"><link rel="prefetch" href="/assets/js/7.58ac7166.js"><link rel="prefetch" href="/assets/js/8.1b9c5002.js"><link rel="prefetch" href="/assets/js/9.5c076362.js">
    <link rel="stylesheet" href="/assets/css/0.styles.5b8fe0a4.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="Jaho’s Blog" class="logo"> <span class="site-name can-hide">Jaho’s Blog</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/posts/" class="nav-link router-link-active">
  技术博客
</a></div> <a href="https://juejin.cn/user/26836627369806" target="_blank" rel="noopener noreferrer" class="repo-link">
    个人主页
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/posts/" class="nav-link router-link-active">
  技术博客
</a></div> <a href="https://juejin.cn/user/26836627369806" target="_blank" rel="noopener noreferrer" class="repo-link">
    个人主页
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><a href="/posts/" aria-current="page" class="sidebar-link">写在前面</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>工作问题解决</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>JavaScript</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/posts/JavaScript/闭包作用域.html" class="sidebar-link">了解闭包及作用域</a></li><li><a href="/posts/JavaScript/短路原则.html" class="sidebar-link">布尔判定和短路原则</a></li><li><a href="/posts/JavaScript/类型转换.html" class="sidebar-link">类型转换</a></li><li><a href="/posts/JavaScript/数组转树.html" class="sidebar-link">数组转树方案</a></li><li><a href="/posts/JavaScript/数组拍平.html" class="sidebar-link">数组拍平方案</a></li><li><a href="/posts/JavaScript/数组求和.html" class="sidebar-link">数组求和（不允许使用循环）</a></li><li><a href="/posts/JavaScript/async.html" class="sidebar-link">async await实战</a></li><li><a href="/posts/JavaScript/defer.html" class="sidebar-link">JS文档的解析:async、defer 属性</a></li><li><a href="/posts/JavaScript/set.html" class="sidebar-link">Set笔记</a></li><li><a href="/posts/JavaScript/map.html" class="sidebar-link">Map笔记</a></li><li><a href="/posts/JavaScript/call.html" class="sidebar-link">手写call、bind、apply</a></li><li><a href="/posts/JavaScript/eventBus.html" class="sidebar-link">Eventbus使用</a></li><li><a href="/posts/JavaScript/instanceof.html" class="sidebar-link">浅析instanceof的实现</a></li><li><a href="/posts/JavaScript/function.html" class="sidebar-link">如何优雅的用function实现一个class</a></li><li><a href="/posts/JavaScript/require.html" aria-current="page" class="active sidebar-link">浅析commonjs中require的执行方式</a></li><li><a href="/posts/JavaScript/promise.html" class="sidebar-link">手写Promise</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Css</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Nodejs</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Vue</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Git</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="浅析commonjs中require的执行方式"><a href="#浅析commonjs中require的执行方式" class="header-anchor">#</a> 浅析commonjs中require的执行方式</h1> <p>熟悉commonjs中的require到底在做一件什么事情？</p> <p>1.require引入一个文件
2.执行文件中的内容
3.抛出一个module.exports</p> <p>先看下面的代码，</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">//1.js</span>

<span class="token keyword">this</span><span class="token punctuation">.</span>a <span class="token operator">=</span> <span class="token number">1111</span>
exports<span class="token punctuation">.</span>b <span class="token operator">=</span> <span class="token number">222</span>
exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">c</span><span class="token operator">:</span><span class="token number">333</span>
<span class="token punctuation">}</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">d</span><span class="token operator">:</span><span class="token number">444</span>
<span class="token punctuation">}</span>
exports<span class="token punctuation">.</span>e <span class="token operator">=</span> <span class="token number">555</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>f <span class="token operator">=</span> <span class="token number">666</span>

<span class="token comment">//2.js</span>
<span class="token keyword">let</span> res <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./1.js'</span><span class="token punctuation">)</span>
</code></pre></div><p>1.js中的代码如果需要执行的话，是需要通过另一个文件对它进行require引用，从而执行内部的逻辑</p> <p>即使单纯的在我们的编辑器中使用run来对代码进行编译的时候，也是通过nodejs实现一个临时文件的方式对1.js来进行require的引用</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token parameter">path</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token operator">...</span><span class="token punctuation">.</span>
    <span class="token comment">//require内部 真正在运行引入文件(模块)的代码</span>
    <span class="token keyword">function</span> <span class="token function">_require</span><span class="token punctuation">(</span><span class="token parameter">exports<span class="token punctuation">,</span>require<span class="token punctuation">,</span>module<span class="token punctuation">,</span>__filename<span class="token punctuation">,</span>__dirname</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">//1.js的代码在这里执行</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> module <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token comment">//这个对象很重要 _require的</span>
        <span class="token literal-property property">exports</span><span class="token operator">:</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> exports <span class="token operator">=</span> module<span class="token punctuation">.</span>exports <span class="token comment">//获取在1.js中返回的对象</span>
    <span class="token function">_require</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>exports<span class="token punctuation">,</span>exports<span class="token punctuation">,</span>require<span class="token punctuation">,</span>module<span class="token punctuation">,</span>__file<span class="token punctuation">,</span>dirname<span class="token punctuation">)</span>  <span class="token comment">//将_require函数用call指定到1.js返回的对象中执行</span>
    <span class="token operator">...</span>

<span class="token keyword">return</span>  module<span class="token punctuation">.</span>exports

<span class="token punctuation">}</span>
</code></pre></div><p>从模仿require内部的实现函数来看，require其实是一个函数，通过reuqire引入的文件会执行在_require的内部中执行，_require传入5个参数，这也是我为什么在commonjs中能够使用exports、module.exports的原因</p> <p>如何使用_require.call来改变this的指向，通过<code>var exports = module.exports</code>,指向的是module中的exports，并且传入的参数也是它，当this的指向改变时，那么在1.js中执行下面的代码</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">//1.js</span>
<span class="token keyword">this</span> <span class="token operator">==</span> module<span class="token punctuation">.</span>exports <span class="token comment">//true</span>
<span class="token keyword">this</span><span class="token operator">==</span> exports <span class="token comment">//true</span>
<span class="token operator">...</span>
</code></pre></div><p><code>当且仅当一开始的时候</code>，this和moudle.exports和exports都是同一个东西</p> <p>当执行1.js的时候,<code>this,module.exports,exports</code>三个被赋上了不一样的值</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">//1.js</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>a <span class="token operator">=</span> <span class="token number">1111</span>
exports<span class="token punctuation">.</span>b <span class="token operator">=</span> <span class="token number">222</span>
exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">c</span><span class="token operator">:</span><span class="token number">333</span>
<span class="token punctuation">}</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">d</span><span class="token operator">:</span><span class="token number">444</span>
<span class="token punctuation">}</span>
exports<span class="token punctuation">.</span>e <span class="token operator">=</span> <span class="token number">555</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>f <span class="token operator">=</span> <span class="token number">666</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>exports<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>module<span class="token punctuation">.</span>exports<span class="token punctuation">)</span>
<span class="token comment">//{ a: 1111, b: 222, f: 666 }</span>
<span class="token comment">//{ c: 333, e: 555 }</span>
<span class="token comment">//{ d: 444 }</span>



<span class="token comment">//2.js</span>
<span class="token keyword">let</span> res <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./2.js'</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
<span class="token comment">//{ d: 444 }</span>
</code></pre></div><p>从2.js中的require引用文件来看，无论是在a中写了this、exports、module.exports，最终的返回结果只会是module.exports中的内容</p> <p>因为在_require中返回的是<code>return module.exports</code>所以我们最终的结果是module.exports中的内容，无论有其他的exports抛出</p></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2023-01-04 10:56:12</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/posts/JavaScript/function.html" class="prev">
        如何优雅的用function实现一个class
      </a></span> <span class="next"><a href="/posts/JavaScript/promise.html">
        手写Promise
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----><!----></div></div>
    <script src="/assets/js/app.6514719e.js" defer></script><script src="/assets/js/2.ea114ffb.js" defer></script><script src="/assets/js/27.204436be.js" defer></script><script src="/assets/js/4.e093c564.js" defer></script>
  </body>
</html>
